package com.travel.WeChatPay;



import com.travel.common.Result;
import com.travel.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("/pay")
public class WxPayController {

    @Autowired
    private WxPayService service;


    /**
     *
     * @author Rick chou
     * @date 2024/7/18 15:33
     * 提交支付
     *
     */
    @GetMapping("/payment/{price}/{payType}")
    public Result<Map<String, Object>> payment(@PathVariable BigDecimal price, @PathVariable String payType) throws Exception {
        Long userId = BaseContext.getCurrentId();
        String orderId = generateOutTradeNo();
        Map<String, Object> result = service.payment(price, orderId, userId,payType);
        return Result.success(result);
    }

    /**
     *
     * @author Rick chou
     * @date 2024/7/15 16:57
     * 微信支付回调
     *
     */


    public String generateOutTradeNo() {
        long timestamp = System.currentTimeMillis();  // 获取当前时间戳
        int randomNum = new Random().nextInt(10000);  // 随机数
        return timestamp + String.format("%04d", randomNum);  // 组合成唯一订单号
    }



}
